import { MenuType } from "@/typings";
import { defineStore } from "pinia";
import { ref } from "vue";

import portService from "@/portService";
import { devMenu } from "@/router/routes/dev";

const useMenuStore = defineStore(
  "menuStore",
  () => {
    /**
     * 有权限的用户菜单
     * @returns
     */
    const authMenu = ref<MenuType[]>([]);
    const setAuthMenu = async () => {
      if (authMenu.value?.length) {
        return authMenu.value;
      }

      const { code, data } = await portService.menuAuthApi();
      if (code === 0) {
        authMenu.value = [...data, ...devMenu];
      }

      return [...data, ...devMenu];
    };

    return {
      authMenu,
      setAuthMenu,
    };
  },
  {
    persist: {
      enabled: true,

      strategies: [
        {
          storage: sessionStorage,
          paths: ["authMenu"],
        },
      ],
    },
  } as any
);

export default useMenuStore;
